# web.creature模块
from fastapi import APIRouter , HTTPException , Depends 

from model.creature import Creature 
from service import creature as service 
from error import Missing, Duplicate 
from service.auth import get_current_active_user , get_current_user


router = APIRouter(prefix='/creature', dependencies=[Depends(get_current_user)]) 


@router.get('/') 
def get_all() -> list[Creature]: 
    try: 
        return service.get_all() 
    except Missing as e: 
        raise HTTPException(status_code=404, detail=e.msg)
    

@router.get('/{name}') 
def get_one(name: str) -> Creature: 
    try:
        return service.get_one(name) 
    except Missing as e: 
        raise HTTPException(status_code=404, detail=e.msg)
    

@router.post('/', status_code=201) 
def create(creature: Creature) -> Creature: 
    try:
        return service.create(creature) 
    except Duplicate as e: 
        raise HTTPException(status_code=404, detail=e.msg)
    

@router.patch('/{name}',status_code=201) 
def modify(name: str, creature: Creature) -> Creature: 
    try:
        return service.modify(name, creature) 
    except Missing as e: 
        raise HTTPException(status_code=404, detail=e.msg)
    except Duplicate as e: 
        raise HTTPException(status_code=404, detail=e.msg)


@router.delete('/{name}') 
def delete(name: str) ->None: 
    try:
        return service.delete(name) 
    except Missing as e: 
        raise HTTPException(status_code=404, detail=e.msg)
    
    
